package guitar

import (
	"context"
	"time"

	"go-weixin/app/guitar/api/internal/svc"
	"go-weixin/app/guitar/api/internal/types"
	"go-weixin/app/guitar/rpc/pb/proto/guitar"

	"github.com/zeromicro/go-zero/core/logx"
)

type GuitarQueryLikeOneFieldLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGuitarQueryLikeOneFieldLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GuitarQueryLikeOneFieldLogic {
	return &GuitarQueryLikeOneFieldLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GuitarQueryLikeOneFieldLogic) GuitarQueryLikeOneField(req *types.GuitarQueryLikeOneFieldReq) (resp *types.GuitarQueryLikeOneFieldResp, err error) {
	ctx, canelFunc := context.WithTimeout(context.Background(), 5*time.Second)
	defer canelFunc()

	rpcResp, err := l.svcCtx.GuitarRPC.GuitarQueryLikeOneField(ctx, &guitar.GuitarQueryLikeOneFieldReq{
		Field: req.Field,
		Query: req.Query,
		Page:  req.Page,
	})
	if err != nil {
		return nil, err
	}
	var guitarImage []types.GuitarJoinImage
	if len(rpcResp.QueryData) > 0 {
		for i := 0; i < len(rpcResp.QueryData); i++ {
			guitarImage = append(guitarImage, types.GuitarJoinImage{
				Id:    rpcResp.QueryData[i].Id,
				Title: rpcResp.QueryData[i].Title,
			})
			// guitarImage[i].Id = rpcResp.QueryData[i].Id
			// guitarImage[i].Title = rpcResp.QueryData[i].Title
			if len(rpcResp.QueryData[i].ImageList) > 0 {
				for _, m := range rpcResp.QueryData[i].ImageList {
					guitarImage[i].ImageList = append(guitarImage[i].ImageList, types.GuitarImage{
						Id:       m.Id,
						Url:      m.Url,
						GuitarId: m.GuitarId,
					})
				}
			}
			// guitarImage[i].ImageList = append(guitarImage[i].ImageList, types.GuitarImage{
			// 	Id: rpcResp.QueryData[i].ImageList,
			// })
		}
		// guitarImage[] = rpcResp.QueryData.Id
		// guitarImage.Title = rpcResp.QueryData.Title
		// for _, m := range rpcResp.QueryData.ImageList {
		// 	guitarImage.ImageList = append(guitarImage.ImageList, types.GuitarImage{
		// 		Id:       m.Id,
		// 		GuitarId: m.GuitarId,
		// 		Url:      m.Url,
		// 	})
		// }
	}
	resp = new(types.GuitarQueryLikeOneFieldResp)
	resp.QueryData = guitarImage
	return resp, err
}
